package dhbw.GUI.Panels;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import Main.MyController;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import dhbw.Data.E_Address;
import dhbw.Data.Language;
import dhbw.Data.Settings;
import dhbw.Roles.User;

public class PanelPersonalien extends JPanel implements ActionListener {

	private boolean writeable;
	private MyController myc;
	private User user;
	private JFrame parent;
	private ContentPanelAddress cp__address;
	private JButton bt_close;

	public PanelPersonalien(boolean writeable, MyController myc, User user,
			JFrame parent) {
		this.writeable = writeable;
		this.myc = myc;
		this.user = user;
		this.parent = parent;
		build();
		makeEditable(writeable);
		fillFields();
	}

	public boolean isWriteable() {
		return writeable;
	}

	public User getUser() {
		return user;
	}

	private void build() {
		this.setBackground(Settings.BG_COLOR);
		this.setLayout(new BorderLayout());
		bt_close= new JButton(Language.CLOSE[myc.getLanguage()]);
		cp__address = new ContentPanelAddress(this.myc);
		bt_close.addActionListener(this);
		this.add("Center",cp__address);
		this.add("South",bt_close);
	}

	private void makeEditable(boolean edit) {
		cp__address.makeEditable(edit);
	}

	private void fillFields() {
		// TODO was ist wenn es keine Adresse gibt?
		cp__address.tf_name.setText(this.user.getName());
		cp__address.tf_vorname.setText(this.user.getVorname());
		if (this.user.getAdresse() != null) {
			cp__address.tf_street.setText(this.user.getAdresse().getStreet());
			cp__address.tf_city.setText(this.user.getAdresse().getCity());
			cp__address.tf_zipcode.setText(this.user.getAdresse().getZipcode());
			cp__address.tf_email.setText(this.user.getAdresse().getEmail());
			cp__address.tf_mobile.setText(this.user.getAdresse().getMobile());
			cp__address.tf_phone.setText(this.user.getAdresse().getPhone());
		}
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getSource().equals(bt_close)) {
			this.parent.setVisible(false);
			this.parent.dispose();
		}
	}
}
